package com.school.campus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.school.common.annotation.Log;
import com.school.common.core.controller.BaseController;
import com.school.common.core.domain.AjaxResult;
import com.school.common.enums.BusinessType;
import com.school.campus.domain.ActivityRegistration;
import com.school.campus.service.IActivityRegistrationService;
import com.school.common.utils.poi.ExcelUtil;
import com.school.common.core.page.TableDataInfo;

/**
 * 活动报名Controller
 *
 * @author wwj
 * @date 2025-11-07
 */
@RestController
@RequestMapping("/campus/registration")
public class ActivityRegistrationController extends BaseController {
    @Autowired
    private IActivityRegistrationService activityRegistrationService;

    /**
     * 查询活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('campus:registration:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActivityRegistration activityRegistration) {
        startPage();
        List<ActivityRegistration> list = activityRegistrationService.selectActivityRegistrationList(activityRegistration);
        return getDataTable(list);
    }

    /**
     * 导出活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('campus:registration:export')")
    @Log(title = "活动报名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ActivityRegistration activityRegistration) {
        List<ActivityRegistration> list = activityRegistrationService.selectActivityRegistrationList(activityRegistration);
        ExcelUtil<ActivityRegistration> util = new ExcelUtil<ActivityRegistration>(ActivityRegistration.class);
        util.exportExcel(response, list, "活动报名数据");
    }

    /**
     * 获取活动报名详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:registration:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(activityRegistrationService.selectActivityRegistrationById(id));
    }

    /**
     * 新增活动报名
     */
    @PreAuthorize("@ss.hasPermi('campus:registration:add')")
    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActivityRegistration activityRegistration) {



        try {
            return toAjax(activityRegistrationService.insertActivityRegistration(activityRegistration));
        }catch (RuntimeException runtimeException) {
            return AjaxResult.error(runtimeException.getMessage());
        }

    }

    /**
     * 修改活动报名
     */
    @PreAuthorize("@ss.hasPermi('campus:registration:edit')")
    @Log(title = "活动报名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActivityRegistration activityRegistration) {
        return toAjax(activityRegistrationService.updateActivityRegistration(activityRegistration));
    }

    /**
     * 删除活动报名
     */
    @PreAuthorize("@ss.hasPermi('campus:registration:remove')")
    @Log(title = "活动报名", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(activityRegistrationService.deleteActivityRegistrationByIds(ids));
    }


    /**
     * 查看我的报名活动列表
     */
    @PreAuthorize("@ss.hasPermi('campus:registration:myList')")
    @GetMapping("/my/{userId}")
    public AjaxResult selectMyActivityRegistrationList(@PathVariable Long userId) {
        return success(activityRegistrationService.selectMyActivityRegistrationList(userId));
    }
}
